package nc.bs.gl.validate;

import nc.bs.common.util.ArrayUtil;
import nc.bs.common.util.DaoUtil;
import nc.bs.common.util.GlDocFreeItem;
import nc.bs.common.util.GlFreeAssistUtil;
import nc.bs.common.util.ObjectUtils;
import nc.jdbc.framework.processor.ColumnProcessor;

import java.util.Objects;
import java.util.function.Predicate;

/**
 * @author duanyp1
 * @date 2024/3/19 10:36
 */
public class SettleAssistUtil {


    public static boolean validateDocFree(String[] origin, String freeCode, String assid) {
        if (assid == null) return false;
        String rs = query(freeCode, assid);
        if(rs == null) return true;
        return ArrayUtil.contain(origin, rs);
    }

    public static boolean validateDocFreePrefix(String[] origin, String freeCode, String assid) {
        if (assid == null) return false;
        String rs = query(freeCode, assid);
        if(rs == null) return true;
        return ArrayUtil.containPrefix(origin, rs);
    }

    public static String buildFreeSQL(String docCode, String assid) {
        GlDocFreeItem item = GlDocFreeItem.getItemByCode(docCode);
        Objects.requireNonNull(item);
        return GlFreeAssistUtil.buildQuerySQL(item.getCode(), item.getColumnCode(), assid);
    }

    public static boolean exist(String freeCode, String assid) {
        return !ObjectUtils.isEmpty(query(freeCode, assid));
    }

    private static String query(String freeCode, String assid) {
        String execSql = buildFreeSQL(freeCode, assid);
        return (String) DaoUtil.executeQuery(execSql, new ColumnProcessor());
    }

}
